package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long id) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        queryWrapper.orderByAsc(Category::getOrderNum);

        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        //查询列表中每条记录是否有子节点，并组装Category对象中的hasChildren属性
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                Long count = baseMapper.selectCount(
                        new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId())
                );
                category.setHasChildren(count > 0);
            }
        }

        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {

        List<Category> categoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        return categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
    }

    @Override
    public List<CategoryVo> tree() {

        //获取一级类别（sql）
        /*for(迭代一级类别){
            //获取二级类别（sql）
            for(迭代二级类别){
              //获取三级类别（sql）
            }
        }*/
        List<Category> categoryList = baseMapper.selectList(null);

        //组装所有类别
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();

        return buildTree(categoryVoList);
    }

    /**
     * 将扁平结构的categoryVoList组装成嵌套结构的List<CategoryVo>
     * @param categoryVoList
     * @return
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {

        ArrayList<CategoryVo> tree = new ArrayList<>();
        for (CategoryVo categoryVo : categoryVoList) {
            if(categoryVo.getParentId().longValue() == 0){
                findAndAddChildren(categoryVoList, categoryVo);
                tree.add(categoryVo);
            }
        }
        return tree;
    }

    private void findAndAddChildren(List<CategoryVo> categoryVoList, CategoryVo categoryVo) {
        for (CategoryVo vo : categoryVoList) {
            if(vo.getParentId().longValue() == categoryVo.getId().longValue()){
                findAndAddChildren(categoryVoList,vo);
                categoryVo.getChildren().add(vo);
            }
        }
    }
}
